package com.bf.electroplating.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bf.electroplating.mapper.PubParamDictionaryMapper;
import com.bf.electroplating.pojo.entity.PubParamDictionary;
import com.bf.electroplating.pojo.vo.ResultVO;
import com.bf.electroplating.pojo.vo.SelectVo;
import com.bf.electroplating.service.PubParamDictionaryService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author 15451
* @description 针对表【pub_param_dictionary(参数字典表)】的数据库操作Service实现
* @createDate 2025-05-09 11:11:32
*/
@Service
public class PubParamDictionaryServiceImpl extends ServiceImpl<PubParamDictionaryMapper, PubParamDictionary>
    implements PubParamDictionaryService{

    @Override
    public ResultVO<List<SelectVo>> getParamList(String moudle ) {
        List<PubParamDictionary> paramDictionaries = list(
                new LambdaQueryWrapper<PubParamDictionary>()
                        .eq(PubParamDictionary::getParamModule, moudle)
                        .and(w -> w.eq(PubParamDictionary::getLevel, "1")
                                .or().isNull( PubParamDictionary::getLevel ))
        );

        List < SelectVo > selectVos = paramDictionaries.stream().map( v -> {
            SelectVo selectVo = new SelectVo();
            selectVo.setName( v.getParamName() );
            selectVo.setId( v.getParamCode() );
            return selectVo;
        } ).collect( Collectors.toList() );
        return ResultVO.ok( selectVos );
    }


    @Override
    public ResultVO < List < SelectVo > > getParamListByMn ( String mn , String moudle ) {
        List<PubParamDictionary> paramDictionaries = list(new LambdaQueryWrapper<PubParamDictionary>()
                .eq(PubParamDictionary::getParamModule, moudle)
                .eq(PubParamDictionary::getParamCode, mn));

        List<SelectVo> result = paramDictionaries.stream().map(item -> {
            SelectVo vo = new SelectVo();
            vo.setId(item.getParamCode());
            vo.setName(item.getParamName());
            // 递归获取子节点
            vo.setSelectVo(getChildren(item.getId (), moudle));
            return vo;
        }).collect(Collectors.toList());
        return ResultVO.ok(result);
    }

    @Override
    public String getCodeByModuleAndName(String module, String name) {
        PubParamDictionary pubParamDictionary=this.getOne(new LambdaQueryWrapper<PubParamDictionary>()
                .eq(PubParamDictionary::getParamModule, module)
                .eq(PubParamDictionary::getParamName, name),false);
        if(pubParamDictionary!=null){
            return pubParamDictionary.getParamCode();
        }else {
            return name;
        }
    }

    @Override
    public String getNameByModuleAndCode(String module, String code) {
        PubParamDictionary pubParamDictionary=this.getOne(new LambdaQueryWrapper<PubParamDictionary>()
                .eq(PubParamDictionary::getParamModule, module)
                .eq(PubParamDictionary::getParamCode, code),false);
        if(pubParamDictionary!=null){
            return pubParamDictionary.getParamName();
        }else {
            return code;
        }
    }

    private List<SelectVo> getChildren(String id, String moudle) {
        List<PubParamDictionary> children = list(new LambdaQueryWrapper<PubParamDictionary>()
                .eq(PubParamDictionary::getParamModule, moudle)
                .eq(PubParamDictionary::getParentCode, id));

        return children.stream().map(item -> {
            SelectVo vo = new SelectVo();
            vo.setId(item.getParamCode());
            vo.setName(item.getParamName());
            // 递归获取子节点
            vo.setSelectVo(getChildren(item.getId (), moudle));
            return vo;
        }).collect(Collectors.toList());
    }


}




